package me.zhengjie.modules.location.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import lombok.AllArgsConstructor;
import me.zhengjie.base.PageInfo;
import me.zhengjie.base.QueryHelpMybatisPlus;
import me.zhengjie.base.impl.CommonServiceImpl;
import me.zhengjie.utils.ConvertUtil;
import me.zhengjie.utils.PageUtil;
import me.zhengjie.modules.location.domain.Location;
import me.zhengjie.modules.location.service.LocationService;
import me.zhengjie.modules.location.service.dto.LocationDto;
import me.zhengjie.modules.location.service.dto.LocationQueryParam;
import me.zhengjie.modules.location.service.mapper.LocationMapper;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
// 默认不使用缓存
//import org.springframework.cache.annotation.CacheConfig;
//import org.springframework.cache.annotation.CacheEvict;
//import org.springframework.cache.annotation.Cacheable;
import org.springframework.data.domain.Pageable;
import java.util.*;

/**
* @author TianI
* @date 2022-01-28
*/
@Service
@AllArgsConstructor
// @CacheConfig(cacheNames = LocationService.CACHE_KEY)
@Transactional(propagation = Propagation.SUPPORTS, readOnly = true, rollbackFor = Exception.class)
public class LocationServiceImpl extends CommonServiceImpl<LocationMapper, Location> implements LocationService {

    // private final RedisUtils redisUtils;
    private final LocationMapper locationMapper;

    @Override
    public PageInfo<LocationDto> queryAll(LocationQueryParam query, Pageable pageable) {
        IPage<Location> queryPage = PageUtil.toMybatisPage(pageable);
        IPage<Location> page = locationMapper.selectPage(queryPage, QueryHelpMybatisPlus.getPredicate(query));
        return ConvertUtil.convertPage(page, LocationDto.class);
    }

    @Override
    public List<LocationDto> queryAll(LocationQueryParam query){
        return ConvertUtil.convertList(locationMapper.selectList(QueryHelpMybatisPlus.getPredicate(query)), LocationDto.class);
    }

    @Override
    public Location getById(Long id) {
        return locationMapper.selectById(id);
    }

    @Override
    // @Cacheable(key = "'id:' + #p0")
    public LocationDto findById(Long id) {
        return ConvertUtil.convert(getById(id), LocationDto.class);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public int insert(LocationDto resources) {
        Location entity = ConvertUtil.convert(resources, Location.class);
        return locationMapper.insert(entity);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public int updateById(LocationDto resources){
        Location entity = ConvertUtil.convert(resources, Location.class);
        int ret = locationMapper.updateById(entity);
        // delCaches(resources.id);
        return ret;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public int removeByIds(Set<Long> ids){
        // delCaches(ids);
        return locationMapper.deleteBatchIds(ids);
    }

    @Override
    public List<Location> getLocation(Long id) {
        List<Location> real_estate_project_id = locationMapper.selectList(new QueryWrapper<Location>().eq("real_estate_project_id", id));
        return real_estate_project_id;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public int removeById(Long id){
        Set<Long> set = new HashSet<>(1);
        set.add(id);
        return this.removeByIds(set);
    }

    /*
    private void delCaches(Long id) {
        redisUtils.delByKey(CACHE_KEY + "::id:", id);
    }

    private void delCaches(Set<Long> ids) {
        for (Long id: ids) {
            delCaches(id);
        }
    }*/

    /*
    @Override
    public void download(List<LocationDto> all, HttpServletResponse response) throws IOException {
      List<Map<String, Object>> list = new ArrayList<>();
      for (LocationDto location : all) {
        Map<String,Object> map = new LinkedHashMap<>();
              map.put("配套所属分类id", location.getLocationCategoryId());
              map.put("配套名称", location.getLocationName());
              map.put("配套距离", location.getLocationDistance());
              map.put("关联房产项目id", location.getRealEstateProjectId());
        list.add(map);
      }
      FileUtil.downloadExcel(list, response);
    }*/
}
